<?php

namespace app\admin\controller;

use think\Controller;

class Index extends Controller
{

    //初始化
    public function initialize()
    {
        if (session('?admin.id')) {
            $this->redirect('admin/home/index');
        }
    }

    //登陆
    public function login()
    {
        if (request()->isAjax()) {
            $data = [
                'username' => input('post.username'),
                'password' => input('post.password')
            ];
            $res = model('Admin')->login($data);
            if ($res == 1) {
                $this->success('登陆成功', 'admin/home/index');
            } else {
                $this->error($res);
            }
        }
        return view();
    }

    //注册
    public function register()
    {
        if (request()->isAjax()) {
            $data = [
                'nickname' => input('post.nickname'),
                'username' => input('post.username'),
                'password' => input('post.password'),
                'conpass' => input('post.conpass'),
                'email' => input('post.email'),
            ];
            $result = model('Admin')->register($data);
            if ($result == 1) {
                $this->success('注册成功', 'admin/index/login');
            } else {
                $this->error($result);
            }
        }

        return view();
    }

    //忘记密码
    public function forget()
    {
        if (request()->isAjax()) {
            $data = [
                'email' => input('post.email'),
            ];
            $result = model('Admin')->forget($data);
            if ($result == 1) {
                $this->success('邮箱存在');
            } else {
                $this->error($result);
            }

        }
        return view();
    }
}
